package com.qf.consumer.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMQConfiguration1 {

    // 消息死信存放的队列
    public final static String deadQueueName = "dead_queue";
    public final static String deadRoutingKey = "dead_routing_key";
    public final static String deadExchangeName = "dead_exchange";
    /**
     * 死信队列 交换机标识符
     */
    public static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";
    /**
     * 死信队列交换机绑定键标识符
     */
    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";
    //声明队列
    @Bean
    public Queue queue1() {
        Map<String, Object> args = new HashMap<>(2);

        args.put(DEAD_LETTER_QUEUE_KEY, deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY, deadRoutingKey);
        Queue queue1 = new Queue("queue1", true, false, false, args);
        //设置队列属性
        return queue1;
    }
    @Bean
    public Queue queue2() {
        Queue queue2 = new Queue("queue2");
        //设置队列属性
        return queue2;
    }
    //声明路由模式交换机
    @Bean
    public DirectExchange ex1() {
        return new DirectExchange("ex1");
    }
    //绑定队列
    @Bean
    public Binding bindingQueue1(Queue queue1, @Qualifier("ex1") DirectExchange ex1) {
        return BindingBuilder.bind(queue1).to(ex1).with("k1");
    }
    @Bean
    public Binding bindingQueue2(Queue queue2, @Qualifier("ex1")DirectExchange ex1) {
        return BindingBuilder.bind(queue2).to(ex1).with("k2");
    }
    /**
     * 消息死信后存放的队列
     * @return
     */
    @Bean("deadQueue")
    Queue deadQueue(){
        Queue queue = new Queue(deadQueueName, true);
        return queue;
    }

    /**
     * 创建死信交换机
     */
    @Bean("deadExchange")
    public DirectExchange deadExchange() {
        return new DirectExchange(deadExchangeName);
    }

    /**
     * 死信队列与死信交换机绑定，指定路由key
     */
    @Bean
    public Binding bindingDeadExchange(Queue deadQueue, DirectExchange deadExchange) {
        return BindingBuilder.bind(deadQueue).to(deadExchange).with(deadRoutingKey);}

//    @Bean
//    public CachingConnectionFactory connectionFactory() {
//        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
//        cachingConnectionFactory.setAddresses("192.168.43.7:5672");
//        cachingConnectionFactory.setUsername("ytao");
//        cachingConnectionFactory.setPassword("admin123");
//        cachingConnectionFactory.setVirtualHost("host1");
//        return cachingConnectionFactory;
//    }
//    @Bean
//    public SimpleMessageListenerContainer simpleMessageListenerContainer(CachingConnectionFactory connectionFactory) {
//        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer( connectionFactory);
//        //设置监听队列
//        container.setQueues(queue1(), queue2());
//        container.setConcurrentConsumers(1);                          //消费者数量
//        container.setMaxConcurrentConsumers(5);                       //最大消费者数量
//        container.setDefaultRequeueRejected(false);                   //重回队列
//        container.setAcknowledgeMode(AcknowledgeMode.AUTO);           //签收模式
//        container.setConsumerTagStrategy(new ConsumerTagStrategy() {  //消费端标签策略
//            @Override
//            public String createConsumerTag(String queue) {
//                return queue+"_"+ UUID.randomUUID();
//            }
//        });
//        container.setMessageListener(new ChannelAwareMessageListener() {
//            @Override
//            public void onMessage(Message message, Channel channel) throws Exception {
//                //做消息的处理
//                String messageBody = new String(message.getBody());
//                System.out.println(messageBody);
//            }
//        });
//        return container;
//    }
}
